﻿/* Copyright (c) 2010, John Cronin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysila
{
    partial class x86_64_Assembler
    {
        IEnumerable<OutputBlock> x86_64_ldobj_r8_sgprptr_dxmm(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            if (!(op1.hardware_loc is x86_64_gpr))
                throw new NotSupportedException();
            return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 0, false, 0, (byte)((op == ThreeAddressCode.Op.ldobj_r8) ? 0xf2 : 0xf3), 0x0f, 0x10));
        }

        IEnumerable<OutputBlock> x86_64_cmp_r8_xmm_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            return OBList(EncOpcode(op1.hardware_loc, op2.hardware_loc, 3, false, 0, new byte[] { 0x66, 0x0f, 0x2f }));
        }

        IEnumerable<OutputBlock> x86_64_assign_r48_simm_dxmm(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            // Load an immediate value to an xmm register

            hardware_location temp_loc = state.reg_alloc.GetTemporaryRegister();
            return OBList(EncAddOpcode(temp_loc as x86_64_gpr, true, 0xb8, ToByteArray((double)op1.constant_val)),
                EncOpcode(result.hardware_loc, temp_loc, 3, (op == ThreeAddressCode.Op.assign_r8) ? true : false, 0, 0x66, 0x0f, 0x7e));
        }

        IEnumerable<OutputBlock> x86_64_assign_r8_xmm_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, false, 0, new byte[] { 0xf2, 0x0f, 0x10 }));
        }

        IEnumerable<OutputBlock> x86_64_assign_r8_xmmmem_xmm(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            return OBList(EncOpcode(op1.hardware_loc, result.hardware_loc, 3, false, 0, new byte[] { 0xf2, 0x0f, 0x11 }));
        }

        IEnumerable<OutputBlock> x86_64_conv_i48r48_gprmem_xmm(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            byte r4 = (byte)(((op == ThreeAddressCode.Op.conv_i4_r4) || (op == ThreeAddressCode.Op.conv_i8_r4) || (op == ThreeAddressCode.Op.conv_i_r4)) ? 0xf3 : 0xf2);
            bool i8 = ((op == ThreeAddressCode.Op.conv_i4_r4) || (op == ThreeAddressCode.Op.conv_i4_r8)) ? false : true;
            return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, i8, 0, new byte[] { r4, 0x0f, 0x2a }));
        }

        IEnumerable<OutputBlock> x86_64_conv_r48i48_dgpr_sxmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            byte r4 = (byte)(((op == ThreeAddressCode.Op.conv_r4_i4) || (op == ThreeAddressCode.Op.conv_r4_i8) || (op == ThreeAddressCode.Op.conv_r4_i)) ? 0xf3 : 0xf2);
            bool i8 = ((op == ThreeAddressCode.Op.conv_r8_i4) || (op == ThreeAddressCode.Op.conv_r4_i4)) ? false : true;
            return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, i8, 0, new byte[] { r4, 0x0f, 0x2d }));
        }

        IEnumerable<OutputBlock> x86_64_sqrt_r8_dxmm_sxmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, false, 0, 0xf2, 0x0f, 0x51));
        }

        IEnumerable<OutputBlock> x86_64_conv_r4r8_dxmm_sxmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, false, 0, 0xf3, 0x0f, 0x5a));
        }

        IEnumerable<OutputBlock> x86_64_conv_r8r4_dxmm_sxmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, false, 0, 0xf2, 0x0f, 0x5a));
        }

        IEnumerable<OutputBlock> x86_64_add_r8_ds1xmm_s2_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(new CodeBlock(EncOpcode(op1.hardware_loc, op2.hardware_loc, 3, false, 0, 0xf2, 0x0f, 0x58), new x86_64_Instruction { opcode = "addsd", Operand1 = op1.hardware_loc, Operand2 = op2.hardware_loc })); }

        IEnumerable<OutputBlock> x86_64_add_r4_ds1xmm_s2_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(new CodeBlock(EncOpcode(op1.hardware_loc, op2.hardware_loc, 3, false, 0, 0xf3, 0x0f, 0x58), new x86_64_Instruction { opcode = "addss", Operand1 = op1.hardware_loc, Operand2 = op2.hardware_loc })); }

        IEnumerable<OutputBlock> x86_64_sub_r8_ds1xmm_s2_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(new CodeBlock(EncOpcode(op1.hardware_loc, op2.hardware_loc, 3, false, 0, 0xf2, 0x0f, 0x5c), new x86_64_Instruction { opcode = "subsd", Operand1 = op1.hardware_loc, Operand2 = op2.hardware_loc })); }

        IEnumerable<OutputBlock> x86_64_sub_r4_ds1xmm_s2_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(new CodeBlock(EncOpcode(op1.hardware_loc, op2.hardware_loc, 3, false, 0, 0xf3, 0x0f, 0x5c), new x86_64_Instruction { opcode = "subss", Operand1 = op1.hardware_loc, Operand2 = op2.hardware_loc })); }

        IEnumerable<OutputBlock> x86_64_mul_r8_ds1xmm_s2_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(new CodeBlock(EncOpcode(op1.hardware_loc, op2.hardware_loc, 3, false, 0, 0xf2, 0x0f, 0x59), new x86_64_Instruction { opcode = "mulsd", Operand1 = op1.hardware_loc, Operand2 = op2.hardware_loc })); }

        IEnumerable<OutputBlock> x86_64_mul_r4_ds1xmm_s2_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(new CodeBlock(EncOpcode(op1.hardware_loc, op2.hardware_loc, 3, false, 0, 0xf3, 0x0f, 0x59), new x86_64_Instruction { opcode = "mulss", Operand1 = op1.hardware_loc, Operand2 = op2.hardware_loc })); }

        IEnumerable<OutputBlock> x86_64_div_r8_ds1xmm_s2_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(new CodeBlock(EncOpcode(op1.hardware_loc, op2.hardware_loc, 3, false, 0, 0xf2, 0x0f, 0x5e), new x86_64_Instruction { opcode = "divsd", Operand1 = op1.hardware_loc, Operand2 = op2.hardware_loc })); }

        IEnumerable<OutputBlock> x86_64_div_r4_ds1xmm_s2_xmmmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(new CodeBlock(EncOpcode(op1.hardware_loc, op2.hardware_loc, 3, false, 0, 0xf3, 0x0f, 0x5e), new x86_64_Instruction { opcode = "divss", Operand1 = op1.hardware_loc, Operand2 = op2.hardware_loc })); }
    }
}
